package Test;

import JaveBean.Manage;
import dao.FindEmpDate;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:TestMybaties2
 * @Date:2025/3/26 11:39
 */
public class TestMybaties2 {
    public static void main(String[] args) throws Exception {
        TestMybaties2 testMybaties2 = new TestMybaties2();
        testMybaties2.insert();
    }
    /**
     * 加载日志
     *
     * @throws Exception
     */
    public void RZ() throws Exception {
        //输出日志信息
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    /**
     * 获取工厂对象
     *
     * @return
     * @throws Exception
     */
    public SqlSession GC() throws Exception {
        //1.读取mybatis配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("config/Mybatiesconfig1.xml");
        //2.读取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //3.通过工厂获取对象
        SqlSession sqlSession = build.openSession();
        return sqlSession;
    }

    /**
     * 查找一条数据
     *
     * @throws Exception
     */
    @Test
    public void findbyid() throws Exception {
        RZ();
        SqlSession gc = GC();
        FindEmpDate mapper = gc.getMapper(FindEmpDate.class);
        Manage findbyid = mapper.findbyid(22);
        System.out.println(findbyid);
    }

    /**
     * 查询所有数据
     *
     * @throws Exception
     */
    @Test
    public void findall() throws Exception {
        RZ();
        SqlSession gc = GC();
        FindEmpDate mapper = gc.getMapper(FindEmpDate.class);
        List<Manage> findall = mapper.findall();
        for (Object obj : findall
        ) {
            System.out.println(obj);
        }
    }

    /**
     * 插入一条数据
     *
     * @throws Exception
     */
    @Test
    public void insert() throws Exception {
        Scanner scanner = new Scanner(System.in);
        Manage manage = new Manage();
        RZ();
        SqlSession gc = GC();
        FindEmpDate mapper = gc.getMapper(FindEmpDate.class);
        System.out.println("请输入admire:");
        String admire1=scanner.next();
        manage.setAdmire(admire1);
        System.out.println("请输入password:");
        String password1=scanner.next();
        manage.setPassword(password1);
        mapper.insert(manage);
        gc.commit();
    }

    /**
     * 删除一条数据
     *
     * @throws Exception
     */
    @Test
    public void deleteone() throws Exception {
        RZ();
        SqlSession gc = GC();
        FindEmpDate mapper = gc.getMapper(FindEmpDate.class);
        mapper.delete(6);
        gc.commit();
    }

    /**
     * 修改数据
     *
     * @throws Exception
     */
    @Test
    public void update() throws Exception {
        RZ();
        SqlSession gc = GC();
        FindEmpDate mapper = gc.getMapper(FindEmpDate.class);
        Manage manage = new Manage();
        manage.setAdmire("黄体酮");
        manage.setPassword("23456");
        manage.setId(3);
        mapper.update(manage);
        gc.commit();
    }
}
